package com.lib.common.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.lib.utils.ActivityLifecycleUtils;
import com.lib.utils.ActivityUtils;
import com.lib.utils.ConvertUtils;


public class KeyboardReplaceView extends View {
    private GlobalLayoutListener mGlobalLayoutListener = new GlobalLayoutListener(this);

    public void setOnKeyboardStateChanged(GlobalLayoutListener.KeyboardLayoutListener listener) {
        mGlobalLayoutListener.setOnKeyboardStateChanged(listener);
    }

    public KeyboardReplaceView(Context context) {
        this(context, null);
    }

    public KeyboardReplaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KeyboardReplaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST) {
            widthSize = getSuggestedMinimumWidth();
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = getSuggestedMinimumHeight();
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mGlobalLayoutListener != null) {
            mGlobalLayoutListener.release();
        }
        super.onDetachedFromWindow();
    }

    /**
     * 可单独使用
     */
    public static class GlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
        private KeyboardLayoutListener mListener;
        private View mView;

        private boolean mNeedChangeSize = true;
        private boolean mIsKeyboardActive = false;

        private ViewGroup.LayoutParams mParams;
        private DisplayMetrics mDisplayMetrics;
        private Rect mRect = new Rect();

        public GlobalLayoutListener(View view) {
            mView = view;
            view.getViewTreeObserver().addOnGlobalLayoutListener(this); /* 监听布局变化 */
            mDisplayMetrics = view.getResources().getDisplayMetrics();
        }

        public void setChangeToKeyBoardHeight(boolean changeSizeWithKeyBoard) {
            mNeedChangeSize = changeSizeWithKeyBoard;
        }

        public void setOnKeyboardStateChanged(KeyboardLayoutListener l) {
            mListener = l;
        }

        public void release() {
            if (mView != null) {
                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mView = null;
            }
            mDisplayMetrics = null;
            mListener = null;
            mParams = null;
            mRect = null;
        }

        @Override
        public void onGlobalLayout() {
            // 获取当前页面窗口的显示范围
            Context context = mView.getContext();
            if (context == null) {
                return;
            }

            ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(mRect);
            float keyboardHeight = mDisplayMetrics.heightPixels - mRect.bottom;
            boolean isActive = keyboardHeight > (mDisplayMetrics.heightPixels / 6.0);

            if (isActive != mIsKeyboardActive) {
                if (mNeedChangeSize) {
                    Activity currentActivity = ActivityLifecycleUtils.getTopActivity();
                    boolean state = false;
                    if (currentActivity != null && android.os.Build.VERSION.SDK_INT > 23) {
                        state = currentActivity.isInMultiWindowMode() || currentActivity.isInPictureInPictureMode();
                    }

                    if (!state) {
                        toChangeSize(context, keyboardHeight);
                    }
                }
                mIsKeyboardActive = isActive;
                if (mListener != null) {
                    mListener.onKeyboardStateChanged(isActive, keyboardHeight);
                }
            }
        }

        private void toChangeSize(Context context, float keyboardHeight) {
            if (mParams == null) {
                mParams = mView.getLayoutParams();
            }
            int heightPixels = mDisplayMetrics.heightPixels;
            int widthPixels = mDisplayMetrics.widthPixels;
            double ratio;
            if (widthPixels > heightPixels) {
                ratio = widthPixels * 1.0 / heightPixels;
            } else {
                ratio = heightPixels * 1.0 / widthPixels;
            }

//           XLog.i("ratio = " + ratio);
            if (ratio > 1.8) {/*大比例的屏幕*/
                keyboardHeight += ConvertUtils.dp2px(context, 40);
            } else {
//                keyboardHeight -= ConvertUtils.dp2px(context, 50);
            }
            mParams.height = (int) keyboardHeight;
            mView.setLayoutParams(mParams);
        }

        public interface KeyboardLayoutListener {
            void onKeyboardStateChanged(boolean isActive, float keyboardHeight);
        }
    }
}